package java.hilyb.record;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import java.hilyb.dbtest.DBHelper;
import java.hilyb.dbtest.R;
import java.hilyb.dbtest.domain.Category;

import java.util.ArrayList;
import java.util.List;

/**
 * User: wlai
 * Date: 1/4/2015
 * Time: 14:50 PM
 */
public class CategoryDetail extends Activity {
    private EditText editText;
    private GridView gridView;
    private Button button1;
    private static List<PicData> picData = new ArrayList<PicData>();
    private DBHelper dbHelper;

    static {
        picData.add(new PicData(1, R.drawable.p1));
        picData.add(new PicData(2, R.drawable.p2));
        picData.add(new PicData(3, R.drawable.p3));
        picData.add(new PicData(4, R.drawable.p4));
        picData.add(new PicData(5, R.drawable.p5));
        picData.add(new PicData(6, R.drawable.p6));

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.category_detail);
        dbHelper = new DBHelper(this);
        editText = (EditText) findViewById(R.id.editText);
        gridView = (GridView) findViewById(R.id.gridView);
        button1 = (Button) findViewById(R.id.button1);
        gridView.setNumColumns(4);
        gridView.setAdapter(new PicItemAdapter(this, picData));
        gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        gridView.setVerticalSpacing(0);
        gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                gridView.setSelection(i);
                BaseAdapter adapter = (BaseAdapter) gridView.getAdapter();
                PicData data = (PicData) adapter.getItem(i);
                for (PicData pd : picData) {
                    pd.setSelected(false);
                }
                data.setSelected(true);
                CheckedTextView ctv = (CheckedTextView)view.findViewById(R.id.label1);
                ctv.setChecked(true);
                adapter.notifyDataSetChanged();
            }
        });

//        gridView.set
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = editText.getText().toString();
                PicData data = (PicData) gridView.getSelectedItem();
                String msg = null;
                if (name == null || "".equals(name.trim())) {
                    msg = "请输入名称";
                } else {
                    if (data == null) {
                        msg = "请选择图片";
                    }
                }
                if (msg != null) {
                    AlertDialog dialog = new AlertDialog.Builder(CategoryDetail.this).create();
                    dialog.setMessage(msg);
                    dialog.show();
                    return;
                }
                Category category = new Category();
                category.setName(name);
                category.setPicId(data.getId());
                dbHelper.addCategory(category);
            }
        });
    }
}


class PicItemAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<PicData> picData;
    private Context context;

    public PicItemAdapter(Context ctx, List<PicData> picData) {
        inflater = LayoutInflater.from(ctx);
        this.context = ctx;
        this.picData = picData;
    }

    @Override
    public int getCount() {
        return picData.size();
    }

    @Override
    public Object getItem(int i) {
        return picData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return picData.get(i).getId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.grid_cell, null);
            PicItemHolder holder = new PicItemHolder();
            ImageView imgView = (ImageView) view.findViewById(R.id.image);
            CheckBox checkBox=(CheckBox)view.findViewById(R.id.checkbox);

            holder.imageView = imgView;
            holder.checkBox=checkBox;
            CheckedTextView ctv = (CheckedTextView) view.findViewById(R.id.label1);
            view.setTag(holder);
        }
        PicData data = (PicData) getItem(i);
        PicItemHolder holder = (PicItemHolder) view.getTag();
       holder.imageView.setImageDrawable(context.getResources().getDrawable(data.getPicId()));
     //   holder.ctv.setText("xxxx");

        if (data.isSelected() == true) {
            view.setBackgroundColor(context.getResources().getColor(R.color.md_yellow_800));
            holder.checkBox.setChecked(true);
        } else {
            view.setBackgroundColor(context.getResources().getColor(R.color.md_black));
            holder.checkBox.setChecked(false);
        }
        return view;
    }
}

class PicItemHolder {
    public ImageView imageView;
    public CheckBox checkBox;
}

class PicData {
    private Integer id;
    private Integer picId;
    private boolean selected = false;

    PicData(Integer id, Integer picId) {
        this.id = id;
        this.picId = picId;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public Integer getId() {
        return id;
    }

    public Integer getPicId() {
        return picId;
    }
}